package org.jeecg.modules.gongqiang.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.gongqiang.entity.SerialNumber;
import org.jeecg.modules.gongqiang.entity.UserhomeOrder;
import org.jeecg.modules.gongqiang.mapper.UserhomeOrderMapper;
import org.jeecg.modules.gongqiang.service.ISerialNumberService;
import org.jeecg.modules.gongqiang.service.IUserhomeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 订单
 * @Author: jeecg-boot
 * @Date: 2024-06-12
 * @Version: V1.0
 */
@Service
public class UserhomeOrderServiceImpl extends ServiceImpl<UserhomeOrderMapper, UserhomeOrder> implements IUserhomeOrderService {

    @Autowired
    private ISerialNumberService serialNumberService;
    private final String BASE_CHECK_CODES = "QWERTYUPLKJHGFDSAZXCVBNM1234567890";

    @Override
    public IPage<UserhomeOrder> pageList(Page<UserhomeOrder> page, QueryWrapper<UserhomeOrder> queryWrapper) {
        return this.baseMapper.getPageList(page, queryWrapper);
    }

    @Override
    public UserhomeOrder getByIdLock(String orderId) {
        return this.baseMapper.getByIdLock(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payStatus(Integer payType, String outTradeNo) {
        UserhomeOrder userhomeOrder = this.baseMapper.getByOrderNumLock(outTradeNo);
        if (userhomeOrder == null) {
            return;
        }
        if (!userhomeOrder.getStatus().equals(0)) {
            return;
        }
        userhomeOrder.setUpdateBy("已付款");
        userhomeOrder.setUpdateTime(new Date());
        userhomeOrder.setRemark("已付款");
        userhomeOrder.setPayType(payType);
        userhomeOrder.setStatus(1);
        userhomeOrder.setPayTime(new Date());
        this.baseMapper.updateById(userhomeOrder);
        List<SerialNumber> serialNumberList = new ArrayList<>();
        for (int i = 0; i < userhomeOrder.getNum(); i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, userhomeOrder.getGoodDetailTermId());
            String number = "XL" + RandomUtil.randomString(BASE_CHECK_CODES, 9)+RandomUtil.randomNumbers(5) + RandomUtil.randomString(BASE_CHECK_CODES, 9) + i + RandomUtil.randomNumbers(4);
            SerialNumber serialNumber = new SerialNumber();
            serialNumber.setCreateBy("自行购买");
            serialNumber.setCreateTime(new Date());
            serialNumber.setUpdateBy("执行序列创建");
            serialNumber.setUpdateTime(new Date());
            serialNumber.setOrderNum(userhomeOrder.getOrderNum());
            serialNumber.setNumber(number);
            serialNumber.setTermId(userhomeOrder.getGoodDetailTermId());
            serialNumber.setBeginTime(new Date());
            serialNumber.setEndTime(calendar.getTime());
            serialNumber.setStatus(1);
            serialNumber.setMachineNum(0);
            serialNumber.setUserId(userhomeOrder.getUserId());
            serialNumber.setGoodId(userhomeOrder.getGoodId());
            serialNumber.setGoodCode(userhomeOrder.getGoodCode());
            serialNumberList.add(serialNumber);
        }
        serialNumberService.saveBatch(serialNumberList);

    }

    @Override
    public void cancelOrder(String orderId, Integer userId) {
        this.baseMapper.cancelOrder(orderId, userId);
    }
}
